/**
 * Copyright (c) 2012 by nrp 
 * All rights reserved.
 */
package util.mail;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;

/**
 * 
 * 文件名称 : SpringMail.java 文件描述 : 使用spring发送邮件 创建人 : 创建日期 : 修改人 : jying 修改日期 : 2014年9月4日 下午2:03:19 版本 : V1.0
 */
public class SpringMail {

	private String type;

	/**
	 * 邮件由admin发出
	 */
	public SpringMail() {

	}

	/**
	 * @param type
	 *            邮件发送者标记: "market" 邮件由市场部发出
	 */
	public SpringMail(String type) {
		this.type = type;
	}

	/**
	 * 发送带附件的邮件
	 * 
	 * @param file
	 * @param mailBean
	 * @throws IOException
	 */

	//	public void sendMimeMail(final File file, final SpringMailBean mailBean) throws IOException {
	//		MailConfigBean config = new MailConfigBean(type);
	//		System.out.println(config);
	//		final String userName = config.getUserName();
	//		String password = config.getPassword();
	//		String host = config.getHost();
	//		final String from = javax.mail.internet.MimeUtility.encodeText(config.getFrom());
	//		JavaMailSenderImpl jmail = new JavaMailSenderImpl();
	//		jmail.setHost(host);
	//		jmail.setUsername(userName);
	//		jmail.setPassword(password);
	//
	//		Properties pp = new Properties();
	//		pp.setProperty("mail.smtp.auth", "true");
	//		jmail.setJavaMailProperties(pp);
	//		MimeMessagePreparator mimeMail = new MimeMessagePreparator() {
	//
	//			public void prepare(MimeMessage mimeMess) throws Exception {
	//				MimeMessageHelper message = new MimeMessageHelper(mimeMess, true, "GBK");
	//
	//				message.setTo(mailBean.getTo());// 设置接收方的email地址
	//				message.setSubject(mailBean.getTitle());// 设置邮件主题
	//				message.setFrom(new InternetAddress(from + " <" + userName + ">"));// 设置发送方地址
	//				message.setText(mailBean.getContext(), true);// 解析为邮件内容为html
	//				if (file != null && file.exists()) {
	//					message.addAttachment(mailBean.getAttName(), file);
	//				}
	//				mimeMess.setSentDate(new Date());
	//			}
	//		};
	//		jmail.send(mimeMail);
	//	}

	public void sendMimeMail(MailConfigBean config, final File[] files, final String[] fileNames, final SpringMailBean mailBean) throws Exception {
		final String userName = config.getUserName();
		String password = config.getPassword();
		String host = config.getHost();
		final String from = javax.mail.internet.MimeUtility.encodeText(config.getFrom());
		JavaMailSenderImpl jmail = new JavaMailSenderImpl();
		jmail.setHost(host);
		jmail.setUsername(userName);
		jmail.setPassword(password);
		Properties pp = new Properties();
		pp.setProperty("mail.smtp.auth", "true");
		jmail.setJavaMailProperties(pp);
		MimeMessagePreparator mimeMail = new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMess) throws Exception {
				MimeMessageHelper message = new MimeMessageHelper(mimeMess, true, "GBK");

				message.setTo(mailBean.getTo());// 设置接收方的email地址
				message.setSubject(mailBean.getTitle());// 设置邮件主题
				message.setFrom(new InternetAddress(from + " <" + userName + ">"));// 设置发送方地址

				//邮件内容加密
				String context = mailBean.getContext();
				message.setText(context, true);// 解析为邮件内容为html
				if (mailBean.getBcc() != null) {
					//message.setBcc(mailBean.getBcc());
					message.setCc(mailBean.getBcc());
				}
				String fileName = null;
				if (files != null && files.length > 0) {
					for (int i = 0; i < files.length; i++) {
						File file = files[i];
						if (file == null || !file.exists())
							continue;
						if (fileNames == null || fileNames[i] == null) {
							fileName = file.getName();
						} else {
							fileName = fileNames[i];
						}
						message.addAttachment(fileName, file);
					}
					mimeMess.setSentDate(new Date());
				}

			}
		};
		jmail.send(mimeMail);
	}

	/**
	 * 发送普通邮件
	 * 
	 * @param mailBean
	 * @throws IOException
	 */
	//	public void sendMimeMail(final SpringMailBean mailBean) throws IOException {
	//		MailConfigBean config = new MailConfigBean(type);
	//		final String userName = config.getUserName();
	//		String password = config.getPassword();
	//		String host = config.getHost();
	//		final String from = javax.mail.internet.MimeUtility.encodeText(config.getFrom());
	//		JavaMailSenderImpl jmail = new JavaMailSenderImpl();
	//		jmail.setHost(host);
	//		jmail.setUsername(userName);
	//		jmail.setPassword(password);
	//		Properties pp = new Properties();
	//		pp.setProperty("mail.smtp.auth", "true");
	//		jmail.setJavaMailProperties(pp);
	//		MimeMessagePreparator mimeMail = new MimeMessagePreparator() {
	//
	//			public void prepare(MimeMessage mimeMess) throws Exception {
	//				MimeMessageHelper message = new MimeMessageHelper(mimeMess, true, "GBK");
	//
	//				message.setTo(mailBean.getTo());// 设置接收方的email地址
	//				message.setSubject(mailBean.getTitle());// 设置邮件主题
	//				message.setFrom(new InternetAddress(from + " <" + userName + ">"));// 设置发送方地址
	//				message.setText(mailBean.getContext(), true);// 解析为邮件内容为html
	//				if (mailBean.getBcc() != null) {
	//					message.setBcc(mailBean.getBcc());
	//				}
	//				mimeMess.setSentDate(new Date());
	//			}
	//		};
	//		jmail.send(mimeMail);
	//	}

	public static void main(String[] args) {
		try {
			SpringMail smail = new SpringMail();

			SpringMailBean mail = new SpringMailBean();
			mail.setContext("你好");
			mail.setTitle("标题");
			mail.setTo(new String[] { "879347604@qq.com" });

			MailConfigBean bean = new MailConfigBean();
			bean.setFrom("rrr88dddc@163.com");
			bean.setHost("smtp.163.com");
			bean.setPassword("");
			bean.setUserName("rrr88dddc@163.com");

			smail.sendMimeMail(bean, null, null, mail);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}